##################################################################
# File : /lua/user/targetarea.lua
# Description : Handle the area targetting visualisation (decal, mesh, whatever)
# Author : GPG
##################################################################

local WorldMesh = import('/lua/user/WorldMesh.lua').WorldMesh

TargetArea = Class() {
    
    # Interface
    SetScreenPosition = function(self,p)
        # It's common to pass in GetMouseScreenPos() which returns nil if mouse is out of the
        # window.
        if not p then
            return
        end
        local wldPos = self.View:UnProject(p)
        # wldPos can be nil if the deprojection fails.
        if not wldPos then
            return
        end
        self.mesh:SetStance( wldPos )
    end,
    
    SetScale = function(self,scale)
        self.mesh:SetScale(Vector(scale,1,scale))
    end,
    
    Show = function(self)
        self.mesh:SetHidden(false)
    end,
    
    Hide = function(self)
        self.mesh:SetHidden(true)
    end,
    
    Destroy = function(self)
        self.mesh:Destroy()
        self.mesh = nil
    end,

    # Internal details
    __init = function(self)
        self.View = import('/lua/ui/game/worldview.lua').View
        self.mesh = WorldMesh()
        self.mesh:SetMesh( 
            { 
                BlueprintID = '/meshes/game/TargetArea_mesh',
                UsePose = false,
            } 
        )
        
        self.mesh:SetStance( Vector(128,0,128) )
        #local manip = self.mesh:GetAnimManip()
        #manip:PlayAnim('todo',true)
        #manip:SetRate(bp.Display.AnimRates.Idle or 1.0)
        
        self.mesh:SetHidden(true)
    end,
}

--[[
# Decal implementation
local UserDecal = import('/lua/user/UserDecal.lua').UserDecal

TargetArea = Class() {
    
    # Interface
    SetScreenPosition = function(self,p)
        if not self.TargetDeacl then self:_create()
        self.TargetDecal:SetPositionByScreen(p)
    end,
    
    SetScale = function(self,scale)
        if not self.TargetDeacl then self:_create()
        self.TargetDecal:SetScale(Vector(scale,1,scale))
    end,
    
    Show = function(self)
        # does nothing in this implementation. we can't hide it so we destroy
        # it and create it on demand.
    end,
    
    Hide = function(self)
        self.TargetDecal:Destroy()
    end,

    # Internal details
    _create = function(self)
        self.TargetDecal = UserDecal {}
        self.TargetDecal:SetTexture("/textures/ui/common/game/AreaTargetDecal/AreaTargetDecal01.dds")
    end,
    
    __init = function(self)
        self:_create()
    end,
}
--]]